package com.lyf.controll;

import cn.hutool.json.JSONObject;
import com.lyf.pojo.Dept;
import com.lyf.pojo.Job;
import com.lyf.result.Code;
import com.lyf.result.Result;
import com.lyf.service.DeptService;
import com.lyf.service.JobService;
import com.lyf.service.impl.DeptServiceImpl;
import com.lyf.service.impl.JobServiceImpl;
import com.lyf.utils.PageModel;
import com.lyf.utils.ResponseResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "JobControll", urlPatterns ={"/job/addOrupdate","/job/delete","/job/selectAll",
        "/job/selectById","/job/queryJobs"})
public class JobControll extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JobService jobService = new JobServiceImpl();
        String servletPath = req.getServletPath();
        System.out.println("请求路径："+servletPath);
        if(servletPath.equals("/job/addOrupdate")){
            String id = req.getParameter("id");
            if(id != null){
                //修改
                String remark = req.getParameter("remark");
                String name = req.getParameter("name");

                Job job = new Job();
                job.setName(name);
                job.setRemark(remark);
                job.setId(Integer.parseInt(id));

                jobService.update(job);

                Result result = new Result(Code.OK,null,"修改成功！");

                ResponseResult.sendResult(resp,result);

            }else{
                //新增
                String remark = req.getParameter("remark");
                String name = req.getParameter("name");

                Job job = new Job();
                job.setName(name);
                job.setRemark(remark);

                jobService.add(job);

                Result result = new Result(Code.OK,null,"新增成功！");
                ResponseResult.sendResult(resp,result);
            }
        }else if(servletPath.equals("/job/delete")){
            String id = req.getParameter("id");

            jobService.delete(Integer.parseInt(id));

            Result result = new Result(Code.OK,null,"删除成功！");
            ResponseResult.sendResult(resp,result);
        }else if(servletPath.equals("/job/selectById")){
            String id = req.getParameter("id");

            Job job = jobService.selectById(Integer.parseInt(id));

            Result result = new Result(Code.OK,job,"查询成功！");
            ResponseResult.sendResult(resp,result);
        }else if(servletPath.equals("/job/queryJobs")){
            //获得当前页码
            String pageIndex = req.getParameter("pageNum");
            //获取每页显示条数
            String pageSize = req.getParameter("pageSize");

            //当前页码
            int pageNum = Integer.parseInt(pageIndex);
            //每页显示条数
            int pageSize2 = Integer.parseInt(pageSize);

            //设置到PageModel
            PageModel model = new PageModel();
            model.setPageIndex(pageNum);
            model.setPageSize(pageSize2);

            //获取查询条件
            String name = req.getParameter("name");

            //查询用户列表
            List<Job> jobs = null;
            Job job = new Job();
            if(!name.equals("") && !name.isEmpty()){
                job.setName(name);
            }

            //总记录数
            int count = jobService.getTotalJobCount(job);
            model.setTotalRecoreSum(count);
            jobs = jobService.queryJob(job, model);

            Map<String,Object> map = new HashMap<>();
            map.put("model",model);
            System.out.println("model:"+model);
            map.put("data",jobs);

            JSONObject jo = new JSONObject();
            jo.putAll(map);
            resp.getWriter().println(jo.toString());//响应到浏览器
        }else if(servletPath.equals("/job/selectAll")){
            List<Job> jobs = jobService.selectAll();

            Result result = new Result(Code.OK,jobs,"查询成功！");
            ResponseResult.sendResult(resp,result);
        }
    }
}
